<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>

    <script>
        $(function () {
            //获取路径参数数组
            var params = getParams();
            if (params.id) {
                //根据路径参数值(攻略id),查询当前攻略的相关数据
                $.get('/strategies/' + params.id, function (data) {
                    //填充数据
                    $(".title").html(data.title);
                });
                //根据攻略id查询当前攻略对应的 攻略目录数据
                $.get('/strategies/' + params.id + '/catalogs', function (data) {
                    //用作序号变量增加
                    var a = 1;
                    //渲染数据
                    $(".catalogs").renderValues({list: data}, {
                        //回调  给攻略目录拼接序号
                        //key fun  所在的标签  及key获取的数据
                        'getName': function (item, value) {
                            //var name = $(item).data("url");
                            $(item).html(a++ + '. ' + value);
                        },
                        //回调  给getHref  函数所在的标签中的属性进行拼接
                        'getHref': function (item, value) {
                            var url = $(item).data("url");
                            $(item).attr('href', url + value);
                        }
                    });
                });


                var currentPage = 1;
                //总页数  用来判断是否是最后一页
                var pages;
                //[] 数组,一个容器 将每次拉倒底部查询的每页数据 数组都存放在里面
                var travelData = [];
                //变量表示 当前页是否已经在查询中 如果是 则不再查询,避免ajax的异步请求发送重复
                var loading = false;//查询状态
                //获取我的游记数据
                function query() {
                    //如果当前为正在查询 则说明ajax已经发送 不需要重复发送
                    if (loading) {
                        return;
                    }
                    //每次在发送ajax前,将查询状态改为正在查询,
                    loading = true;
                    //发送请求查询当前攻略 的 评论信息
                    $.get('/strategies/' + params.id + '/comments', {currentPage: currentPage}, function (data) {
                        //将查询出来的需要的数据的数组放到容器中
                        $.merge(travelData, data.list);
                        console.log(data.list);
                        //渲染数据
                        $("#comment").renderValues({list: travelData}, {
                            //获取给starNum 所在的标签 及其render-key所获取的starNum的值
                            'getStar': function (item, value) {
                                //拼接所有评论的星级标签
                                var temp = "";
                                //对得到的starNum进行循环,拼出评论的星数
                                for (var i = 0; i < 5; i++) {
                                    //如果i小于当前查询出来星数,则应该是实心星星
                                    if (i < value) {
                                        temp += "<i class='fa fa-star'></i>";
                                    } else {//否则空心 星星
                                        temp += '<i class="fa fa-star-o"></i>';
                                    }
                                }
                                //将拼接的星星结果标签元素,拼接到指定位置
                                $(item).append(temp);

                            },
                            'getHref': function (item, value) {
                                var url = $(item).data('url');
                                $(item).attr('href', url + value);
                            },
                            'getId': function (item, value) {
                                $(item).attr('href', "strategyComment.html?id=" + value);
                                console.log(value);
                            }
                        });
                        //给图片绑定事件
                        $(".comment-img img").click(function () {
                            var url = $(this).attr("src");
                            $(document).dialog({
                                titleShow: false,
                                overlayClose: true,
                                content: '<img src="' + url + '" width="100%"/>'
                            });
                        });
                        //当前页加1 下次将查询第二页数据
                        currentPage++;
                        //总页数
                        pages = data.pages;
                        //查询请求结果,则将查询状态改为 不在查询中
                        loading = false;
                    });
                }

                //默认的第一次查第一页数据
                query();
                //给window绑定滚动事件,当window滚动式,调用其中的函数
                $(window).scroll(function () {
                    //判断是否滚动到底部(屏幕高度+HTML滚动超出顶部的高度>=HTML文档的高度)
                    if ($(window).height() + $(document).scrollTop() + 1 >= $(document).height()) {
                        //滚动到当前文档底部,并且是否还有页数可以显示 有则继续查询出来显示
                        if (currentPage <= pages) {
                            query();
                            //滚动到底部时,如果此时还有页数 弹出加载中信息
                            $(document).dialog({
                                type: 'notice',
                                infoText: '加载中',
                                autoClose: 1500,
                                position: 'center'  // center: 居中; bottom: 底部
                            });
                        } else {
                            //滚动到当前文档底部,如果已经是最后一页 则弹出已经到底
                            $(document).dialog({
                                type: 'notice',
                                infoText: '亲,已经到底了',
                                autoClose: 1500,
                                position: 'bottom'  // center: 居中; bottom: 底部
                            });
                        }
                    }

                });
                $("#commentBtn").click(function () {
                    window.location.href = "/mine/addComment.html?id=" + params.id;
                });
            }

            /*===============================收藏======================================*/
            var user = JSON.parse(sessionStorage.getItem("user"));
            var userId;
            if (!user) {
                userId = null;
            } else {
                userId = user.id;
            }
            var type = $("#collectBtn").data("type");

            //回显该文章的被收藏数量
            $.get("/collect/count", {contentId: params.id, type: type}, function (data) {
                $("#collectBtn span").html(data.count);
            })

            //回显收藏
            $.get("/collect/select", {userId: userId, contentId: params.id, type: type}, function (data) {
                if (data.success) {
                    console.log("收藏了,回显实心");
                    $("#collectBtn").removeClass("fa-heart-o").addClass("fa-heart")
                } else {
                    console.log("没收藏,回显空心");
                    $("#collectBtn").removeClass("fa-heart").addClass("fa-heart-o")
                }
            })

            //收藏的星星按钮绑定点击事件(收藏/取消收藏)
            $("#collectBtn").click(function () {
                //获取当前文章的收藏数
                var count = $("#collectBtn span").html();
                //验证是否登录了
                if (!user) {
                    //没登录就弹提示框确认是否跳转登录
                    $(document).dialog({
                        type: 'confirm',
                        closeBtnShow: true,
                        content: '请登陆后再执行相关操作!',
                        onClickConfirmBtn: function () {
                            window.location.href = "/login.html"
                        }
                    });
                } else {
                    //登录了就发送请求进行处理
                    $.get("/collect", {userId: userId, contentId: params.id, type: type}, function (data) {
                        if (data.success) {
                            console.log("设置收藏");
                            //设置收藏,回显实心
                            $("#collectBtn").removeClass("fa-heart-o").addClass("fa-heart")
                            $("#collectBtn span").html(count * 1 + 1);
                        } else {
                            console.log("取消收藏");
                            //取消收藏,回显空心
                            $("#collectBtn").removeClass("fa-heart").addClass("fa-heart-o")
                            $("#collectBtn span").html(count - 1);

                        }
                    })
                }
            })
            /*==============================点赞======================================*/
            type = $("#likeBtn").data("type");

            //回显该文章的被收藏数量
            $.get("/dogood/count", {contentId: params.id, type: type}, function (data) {
                $("#likeBtn span").html(data.count);
            })

            //回显点赞
            $.get("/dogood/select", {userId: userId, contentId: params.id, type: type}, function (data) {
                if (data.success) {
                    console.log("点赞了,回显实心拇指");
                    $("#likeBtn").removeClass("fa-thumbs-o-up").addClass("fa-thumbs-up")
                } else {
                    console.log("没点赞,回显空心拇指");
                    $("#likeBtn").removeClass("fa-thumbs-up").addClass("fa-thumbs-o-up")
                }
            })

            //点赞的拇指按钮绑定点击事件(点赞/取消点赞)
            $("#likeBtn").click(function () {
                //获取当前文章的收藏数
                var count = $("#likeBtn span").html();
                //验证是否登录了
                if (!user) {
                    //没登录就弹提示框确认是否跳转登录
                    $(document).dialog({
                        type: 'confirm',
                        closeBtnShow: true,
                        content: '请登陆后再执行相关操作!',
                        onClickConfirmBtn: function () {
                            window.location.href = "/login.html"
                        }
                    });
                } else {
                    //登录了就发送请求进行处理
                    $.get("/dogood", {userId: userId, contentId: params.id, type: type}, function (data) {
                        if (data.success) {
                            console.log("设置点赞");
                            //设置点赞,回显实心拇指
                            $("#likeBtn").removeClass("fa-thumbs-o-up").addClass("fa-thumbs-up")
                            $("#likeBtn span").html(count * 1 + 1);
                        } else {
                            console.log("取消收藏");
                            //取消点赞,回显空心拇指
                            $("#likeBtn").removeClass("fa-thumbs-up").addClass("fa-thumbs-o-up")
                            $("#likeBtn span").html(count - 1);

                        }
                    })
                }
            })
        });
    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜索旅行攻略">
            </div>
        </div>
    </div>
</div>

<div class="container strategies">
    <h5 class="title"></h5>
    <!--<small>
        <span>0</span>人阅读过此攻略
    </small>-->

    <div class="focus">
        <i class="fa fa-heart-o" id="collectBtn" data-type="1"><span></span>收藏</i>
        <i class="fa fa-thumbs-o-up" id="likeBtn" data-type="1"><span></span>点赞</i>
        <button class="btn">想去</button>
        <button class="btn">去过</button>
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">
        <div class="container hot">
            <h6>大家都在看</h6>
            <div>
                <a href="travelContent.html">
                    <button class="btn">上下九</button>
                </a><a href="travelContent.html">
                <button class="btn">入住长隆熊猫酒店，天天都是儿童节</button>
            </a><a href="travelContent.html">
                <button class="btn">飞到羊城，只为一场最地道的早茶之旅</button>
            </a></div>
        </div>
        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-key="list.name" render-fun="getName"></h6>
                    <div render-loop="list.details">
                        <a data-url="strategyDetails.html?detailId=" render-key="list.details.id" render-fun="getHref">
                            <button class="btn" render-html="list.details.title"></button>
                        </a>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div>
                <span>价格便宜</span>
                <span>很热闹</span>
                <span>交通方便</span>
                <span>吃的很多</span>
                <span>风景优美</span>
                <span>美食很多</span>
                <span>很特别</span>
                <span>爱上这座城</span>
                <span>住宿方便</span>
                <span>刚刚</span>
            </div>
        </div>
        <hr>

        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a data-url="userProfiles.html?id=" render-key="list.author.id" render-fun="getHref">
                            <img class="rounded-circle"
                                 render-src="list.author.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.author.nickName"></span></p>
                        <span class="comment-star" render-key="list.starNum" render-fun="getStar">
                            <!--<i class="fa fa-star"></i>
                            <i class="fa fa-star-o"></i>-->
                        </span>
                        <span class="comment-date" render-html="list.createTime"></span>
                        <div class="comment-content" id="setset">
                            <a render-key="list.id" render-fun="getId">
                                <p render-html="list.content"></p>
                            </a>
                        </div>

                        <ul class="comment-img" render-loop="list.imgArr">
                            <li>
                                <img render-src="list.imgArr.self">
                            </li>
                        </ul>

                        <div class="d-flex icon">
                            <a render-key="list.id" render-fun="getId">
                                <i class="fa  fa-commenting-o commentBtn" style="margin-right: 10px"
                                render-html="list.strategyCommentNum"></i>评论
                                &nbsp;<i class="fa fa-thumbs-o-up"> <span></span>0 </i>
                            </a>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>

</div>
</div>

<div class="operation">
    <button class="btn">加入旅行单</button>
    <button class="btn" id="commentBtn">点评</button>
</div>


</body>
</html>